package com.sphd.restaurantsystem.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sphd.restaurantsystem.R;
import com.sphd.restaurantsystem.base.BaseActivity;
import com.sphd.restaurantsystem.info.LoginInfo;
import com.sphd.restaurantsystem.info.RestaurantInfo;
import com.sphd.restaurantsystem.utils.AppManager;
import com.sphd.restaurantsystem.utils.Constant;
import com.wzh.frame.views.RoundImageView;

import org.litepal.crud.DataSupport;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 个人设置
 */
public class MyselfSettingActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.imageView_title)
    RoundImageView imageViewTitle;
    @BindView(R.id.img_backgrund)
    LinearLayout imgBackgrund;
    @BindView(R.id.logon_name)
    TextView logonName;
    @BindView(R.id.logon_birthday)
    TextView logonBirthday;
    @BindView(R.id.logon_age)
    TextView logonAge;
    @BindView(R.id.logon_restaurant_num)
    TextView logonRestaurantNum;
    @BindView(R.id.back_button)
    Button backButton;
    @BindView(R.id.content_myself_setting)
    NavigationView contentMyselfSetting;
    Intent intent;
    Bundle bundle;
    RestaurantInfo restaurantInfo;
    LoginInfo loginInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myself_setting);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(v -> finish());
    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        restaurantInfo = (RestaurantInfo) bundle.getSerializable(Constant.RESTAURANTSINFO);
        loginInfo = DataSupport.findLast(LoginInfo.class);
        setTitleBar(toolbar, "个人设置");
        logonName.setText(loginInfo.getStaff_name());
        logonBirthday.setText("生日："+loginInfo.getStaff_regtime());
        logonRestaurantNum.setText("店铺数量："+loginInfo.getCgnumber() + "家");
        setImage(imageViewTitle, loginInfo.getStaff_photo());
    }

    @OnClick({R.id.imageView_title, R.id.back_button})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.imageView_title:
                break;
            case R.id.back_button:
                finishDialog();
                break;
        }
    }

    private void finishDialog() {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context);
        alertDialogBuilder
                .setIcon(R.mipmap.ic_launcher_small)
                .setTitle("提示")
                .setMessage("确定要退出帐号吗?")
                .setCancelable(false)
                .setPositiveButton("确定",
                        (dialog, id) -> {
                            AppManager.getAppManager().delAllActivityExceptMain(MyselfSettingActivity.class);
                            DataSupport.deleteAll(LoginInfo.class);
                            remove(Constant.ISLOGIN);
                            remove(Constant.ISREMBER);
                            intent.setClass(context, LoginActivity.class);
                            startActivity(intent);
                            finish();

                        })
                .setNegativeButton("取消",
                        (dialog2, id) -> dialog2.cancel());
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }
}
